package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-03-19 19:11
 **/
public class LeetCode606 {

    public String tree2str(TreeNode root) {
        if (root == null) {
            return "";
        }

        if (root.left == null && root.right == null) {
            return String.valueOf(root.val);
        }

        if (root.right == null) {
            return String.valueOf(root.val) + '(' + tree2str(root.left) + ')';
        }

        return String.valueOf(root.val) + '(' + tree2str(root.left) + ')' + '(' + tree2str(root.right) + ')';
    }

}
